package net.cyndeline.misc


/**
 * Maps lists of values against a key.
 */
class ListMapper[A, B] {
  private var register = Map[A, List[B]]()
  
  def items(a: A) = register getOrElse(a, Nil)
  
  def add(mapping: (A, B)) {
    val a = mapping._1
    val b = mapping._2
    if ((register contains a) && (!(register(a) contains b)))
      register += (a -> (register(a) :+ b))
    else
      register += (a -> (b :: Nil))
  }
  
  def remove(b: B, a: A) {
    if ((register contains a) && (register(a) contains b)) {
      register += (a -> (register(a) filter (_ != b)))
    }
  }
}